package org.farm2.service.event;

import com.farm2.lucene.Farm2LuceneFace;
import com.farm2.lucene.document.FarmDocument;
import com.farm2.lucene.impl.Farm2LuceneIndexImpl;
import com.farm2.lucene.inter.Farm2LuceneIndexInter;
import com.farm2.lucene.utils.FarmLuceneUtils;
import lombok.extern.slf4j.Slf4j;
import org.farm2.base.domain.FarmUserContext;
import org.farm2.base.event.F2Event;
import org.farm2.base.event.enums.F2EActionT;
import org.farm2.base.event.enums.F2EObjectT;
import org.farm2.base.event.inter.Farm2EventHandle;
import org.farm2.files.domain.ResourceFile;
import org.farm2.files.service.ResourceFileServiceInter;
import org.farm2.files.utils.Farm2RegisteTypeEnum;
import org.farm2.files.utils.FileDownloadUtils;
import org.farm2.service.event.utils.KnowIndexDocumentBuilder;
import org.farm2.service.inter.FarmUserServiceInter;
import org.farm2.service.lucene.doc.KnowIndexDocument;
import org.farm2.service.lucene.doc.enums.DocumentTitleEnum;
import org.farm2.service.lucene.doc.enums.IndexModelEnum;
import org.farm2.skc.domain.SkcKnow;
import org.farm2.skc.dto.web.IndexTagsDto;
import org.farm2.skc.service.SkcKnowServiceInter;
import org.farm2.tools.files.Farm2FileUtils;
import org.farm2.tools.web.Farm2HtmlUtils;
import org.farm2.wdap.convertor.filemode.impl.TextFileMode;
import org.farm2.wdap.convertor.filemode.inter.FileModel;
import org.farm2.wdap.domain.WdapExtendFile;
import org.farm2.wdap.utils.FileModelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.farm2.wdap.service.WdapExtendFileServiceInter;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 创建附件索引
 */
@Slf4j
@Service
public class EventLuceneIndexHandle implements Farm2EventHandle {
    @Autowired
    private ResourceFileServiceInter resourceFileServiceImpl;
    @Autowired
    private FileDownloadUtils fileDownloadUtils;
    @Autowired
    private WdapExtendFileServiceInter wdapExtendFileServiceImpl;
    @Autowired
    private FarmUserServiceInter farmUserService;
    @Autowired
    private SkcKnowServiceInter skcKnowServiceImpl;
    @Autowired
    private KnowIndexDocumentBuilder knowIndexDocumentBuilder;


    @Override
    public void handle(F2Event event) {
        if (event.getAction().getType().equals(F2EActionT.UPDATE) && event.getObj().getType().equals(F2EObjectT.KNOW)) {
            String knowId = event.getObj().getId();

            Farm2LuceneIndexInter indexService = Farm2LuceneFace.getIndexService();
            indexService.deleteIndex(List.of(knowId));
            SkcKnow know = skcKnowServiceImpl.getSkcKnowById(knowId);

            indexKnow(knowId, know);

            List<String> files = skcKnowServiceImpl.getInvalidVersionFileIds(know);
            indexService.deleteIndex(files);
        }
        if (event.getAction().getType().equals(F2EActionT.DELETE) && event.getObj().getType().equals(F2EObjectT.KNOW)) {

            String knowId = event.getObj().getId();
            Farm2LuceneIndexInter indexService = new Farm2LuceneIndexImpl(FarmLuceneUtils.getLuceneIndexFilePath());
            indexService.deleteIndex(List.of(knowId), DocumentTitleEnum.knowId.name());
        }
        if (event.getAction().getType().equals(F2EActionT.ADD) && event.getObj().getType().equals(F2EObjectT.KNOW)) {

            String knowId = event.getObj().getId();
            SkcKnow know = skcKnowServiceImpl.getSkcKnowById(knowId);
            if (know != null) {
                indexKnow(knowId, know);
            }
        }

        if (event.getAction().getType().equals(F2EActionT.EXTEND_FILE)) {
            try {
                String fileId = event.getObj().getId();
                ResourceFile rFile = resourceFileServiceImpl.getResourceFileById(fileId);
                String knowId = resourceFileServiceImpl.getBindAppId(rFile.getId(), Farm2RegisteTypeEnum.KNOW);
                Farm2LuceneIndexInter indexService = new Farm2LuceneIndexImpl(FarmLuceneUtils.getLuceneIndexFilePath());
                indexService.deleteIndex(List.of(fileId));
                List<FarmDocument> docs = new ArrayList<>();
                FarmDocument doc = knowIndexDocumentBuilder.getFileDoc(knowId, fileId).getDoc();
                if (doc != null) {
                    docs.add(doc);
                }
                indexService.createIndex(docs);
            } catch (IOException e) {
                log.error(e.getMessage(), e);
            }
        }

        if (event.getAction().getType().equals(F2EActionT.CANCEL_FILE)) {
            String fileId = event.getObj().getId();
            Farm2LuceneIndexInter indexService = new Farm2LuceneIndexImpl(FarmLuceneUtils.getLuceneIndexFilePath());
            indexService.deleteIndex(List.of(fileId));
        }
    }

    private void indexKnow(String knowId, SkcKnow know) {
        Farm2LuceneIndexInter indexService = new Farm2LuceneIndexImpl(FarmLuceneUtils.getLuceneIndexFilePath());
        indexService.deleteIndex(List.of(knowId));
        List<FarmDocument> docs = new ArrayList<>();
        KnowIndexDocument doc = knowIndexDocumentBuilder.getKnowDoc(knowId);
        if (doc != null) {
            docs.add(doc.getDoc());
        }
        indexService.createIndex(docs);
    }

}
